#include "permutation_combination_api.h"
#include "permutation.h"
#include "combination.h"
#include <stdio.h>
#include "base_math.h"

uint32_t gbCaseFailed = 0;

#define EXPECT_EQ(expect, actual) if((expect) != (actual)) {\
	printf("[FAILED][%s:%d] actual(%d) diff with expect(%d).\n", __FUNCTION__, __LINE__, actual, expect);\
	gbCaseFailed = 1;\
} else {\
	;\
}


void permutation_test() {
#if 0
	uint8_t per[2];
	uint32_t ret;
	ret = GetPermutation(2, 0, per, 2);
	EXPECT_EQ(0, ret);
	EXPECT_EQ(0, per[0]);
	EXPECT_EQ(1, per[1]);


	ret = GetPermutation(2, 1, per, 2);
	EXPECT_EQ(0, ret);
	EXPECT_EQ(1, per[0]);
	EXPECT_EQ(0, per[1]);
#endif
}

void combination_test() {
	uint8_t arr[100] = { 0 };
	Ci(2, 1, 0, arr);
	EXPECT_EQ(0, arr[0]);

	Ci(2, 1, 1, arr);
	EXPECT_EQ(1, arr[0]);

	Ci(2, 2, 0, arr);
	EXPECT_EQ(0, arr[0]);
	EXPECT_EQ(1, arr[1]);

	Ci(3, 1, 0, arr);
	EXPECT_EQ(0, arr[0]);

	Ci(3, 1, 1, arr);
	EXPECT_EQ(1, arr[0]);

	Ci(3, 1, 2, arr);
	EXPECT_EQ(2, arr[0]);

	Ci(3, 2, 0, arr);
	EXPECT_EQ(0, arr[0]);
	EXPECT_EQ(1, arr[1]);

	Ci(3, 2, 1, arr);
	EXPECT_EQ(0, arr[0]);
	EXPECT_EQ(2, arr[1]);

	Ci(3, 2, 2, arr);
	EXPECT_EQ(1, arr[0]);
	EXPECT_EQ(2, arr[1]);

	Ci(3, 3, 0, arr);
	EXPECT_EQ(0, arr[0]);
	EXPECT_EQ(1, arr[1]);
	EXPECT_EQ(2, arr[2]);

	Ci(4, 1, 0, arr);
	EXPECT_EQ(0, arr[0]);

	Ci(4, 1, 3, arr);
	EXPECT_EQ(3, arr[0]);

	Ci(4, 2, 0, arr);
	EXPECT_EQ(0, arr[0]);
	EXPECT_EQ(1, arr[1]);

	Ci(4, 2, 5, arr);
	EXPECT_EQ(2, arr[0]);
	EXPECT_EQ(3, arr[1]);

	Ci(4, 4, 0, arr);
	EXPECT_EQ(0, arr[0]);
	EXPECT_EQ(1, arr[1]);
	EXPECT_EQ(2, arr[2]);
	EXPECT_EQ(3, arr[3]);

	Ci(32, 1, 0, arr);
	EXPECT_EQ(0, arr[0]);

	Ci(32, 1, 31, arr);
	EXPECT_EQ(31, arr[0]);

	Ci(32, 31, 0, arr);
	EXPECT_EQ(0, arr[0]);
	EXPECT_EQ(1, arr[1]);
	EXPECT_EQ(2, arr[2]);
	EXPECT_EQ(30, arr[30]);


	Ci(32, 32, 0, arr);
	EXPECT_EQ(0, arr[0]);
	EXPECT_EQ(1, arr[1]);
	EXPECT_EQ(2, arr[2]);
	EXPECT_EQ(31, arr[31]);

}


int main() {
	permutation_test();
	combination_test();

	if (!gbCaseFailed) {
		printf("ALL success!\n");
	}
	return 0;
}



